package com.example.demo.publisher;

import com.example.demo.entity.LogUsual;
import com.example.demo.event.UsualLogEvent;
import com.example.demo.util.SpringUtil;
import com.example.demo.util.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 日志信息事件发送
 *
 * @author cici
 */
public class UsualLogPublisher {

	@Autowired
	private ApplicationContext context;

	public void publishEvent(String level, String id, String data) {
		HttpServletRequest request = WebUtil.getRequest();
		LogUsual logUsual = new LogUsual();
		logUsual.setLogLevel(level);
		logUsual.setLogId(id);
		logUsual.setLogData(data);
		Thread thread = Thread.currentThread();
		StackTraceElement[] trace = thread.getStackTrace();
		if (trace.length > 3) {
			logUsual.setMethodClass(trace[3].getClassName());
			logUsual.setMethodName(trace[3].getMethodName());
		}
		Map<String, Object> event = new HashMap<>(16);
		event.put("log", logUsual);
		context.publishEvent(new UsualLogEvent(event));
	}

}
